package org.s3.project.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.apache.ibatis.annotations.*;
import org.s3.project.entity.Favorites;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * @author ljh
 * @version 1.0
 * @Description
 * @date 2025/2/18 09:16
 */
@Mapper
@Repository
public interface FavoritesMapper extends BaseMapper<Favorites> {
    //查询收藏
    List<Favorites> selectFavorites(int id);
    //通过修改取消收藏
    @Delete("delete from favorites  \n" +
            "where dish_id=#{dish_id}")
    int deleteFavorites(int dish_id);
    //新增收藏
    @Insert("insert into favorites (user_id, dish_id,create_time, update_time, status)\n" +
            "value(#{favorites.user_id}, #{favorites.dish_id}, #{favorites.create_time}, #{favorites.update_time},1);")
    Integer getFavoritesAdd(@Param("favorites")Favorites favorites);
    //唯一验证
    @Select("select count(*) as count\n" +
            "from favorites\n" +
            "where user_id=#{favorites.user_id} and dish_id = #{favorites.dish_id};\n")
    Integer selectFavoritesByUserIdAndDishId(@Param("favorites") Favorites favorites);

    //删除收藏
    @Delete("delete from favorites\n" +
            "where user_id=#{user_id} and dish_id=#{dish_id};\n")
    Integer deleteFavorites(@Param("user_id")Integer user_id, @Param("dish_id")Integer dish_id);

    //查询收藏数量
    @Select("select d.name, d.price, m.name as mname,d.image,f.user_id,f.dish_id\n" +
            "        from `user` u,\n" +
            "             dish d,\n" +
            "             favorites f,\n" +
            "             merchant m,\n" +
            "             category c\n" +
            "        where f.dish_id = d.id\n" +
            "          and d.category_id = c.id\n" +
            "          and c.merchant_id = m.id\n" +
            "          and f.user_id = u.id\n" +
            "          and f.status = 1\n" +
            "          and user_id = #{user_id}")
    List<Favorites> selectFavoritesCount(int user_id);
    //查询已收藏的菜品id
    List<Favorites> getFavoritesId(int user_id);
}
